package chapter1.service.array;

import chapter1.domain.entity.Dept;
import chapter1.domain.entity.Role;
import chapter1.domain.entity.User;
import chapter1.domain.entity.dto.UserDto;
import chapter1.domain.entity.vo.Result;
import chapter1.domain.entity.vo.UserVo;
import chapter1.service.IDeptService;
import chapter1.service.IRoleService;
import chapter1.service.IUserService;
import chapter1.util.SingletonFactory;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * 业务层-用户
 *
 * @author 郑植航
 * @since 2025/5/17 16:25
 */
@Deprecated
public class UserService extends BaseService<User,UserDto,UserVo>implements IUserService {


    private final IDeptService deptService;
    private final IRoleService roleService;




    private UserService() {
        deptService= SingletonFactory.getInstance(DeptService.class);
        roleService= SingletonFactory.getInstance(RoleService.class);
        data = new User[DATA_SIZE+1];
        temp = new UserVo[DATA_SIZE];
    }
    @Override
    UserVo convertToVo(User user){
        String  deptName = "";
        Dept  dept = deptService.findById(user.deptId);
        if(dept!=null){
            deptName = dept.name;
        }
        String  roleName = "";
        Role  role = roleService.findById(user.roleId);
        if(role!=null){
            roleName = role.name;
        }
        return new UserVo(user.getId(),user.createAt,user.updateAt,user.username,user.name,user.mobile,user.email,deptName,roleName,user.deptId,user.roleId,user.enable);
     }


    @Override
    User createEntity(UserDto dto, int id){
        LocalDateTime now = LocalDateTime.now();
        return new User(id,now,now,dto.username,dto.name,dto.mobile,dto.email,dto.deptId,dto.roleId);
    }
    @Override
    void updateEntity(UserDto dto, User user){
        user.updateAt=LocalDateTime.now();
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }
    @Override
    public  boolean deptInUse(int deptId){
        boolean  inUse = false;
        for(User user:data){
            if(user!=null && user.deptId==deptId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }
    @Override
    public  boolean roleInUse(int roleId){
        boolean  inUse = false;
        for(User user:data){
            if(user!=null && user.deptId==roleId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }


    @Override
    public  Result create(UserDto dto) {

        if(!deptService.existById(dto.deptId)){
            return Result.failure("创建失败，不存在指定的部门");
        }
        if(!roleService.existById(dto.roleId)){
            return Result.failure("创建失败，不存在指定的角色");
        }
       return super.create(dto);
    }
    @Override
    public  Result update(UserDto dto,int id) {

        if(!deptService.existById(dto.deptId)){
            return Result.failure("更新失败，不存在指定的部门");
        }
        if(!roleService.existById(dto.roleId)){
            return Result.failure("更新失败，不存在指定的角色");
        }
        return super.update(dto,id);
    }
    @Override
    public  Result toggle(int id){
        if (!existById(id)) {
            return Result.failure("切换状态失败，不存在该数据");
        }
        data[id].enable = !data[id].enable;
        return Result.success(data[id].enable ? "启用成功" :"禁用成功", null);
    }
}
